﻿#if NETCore
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Primitives;
#endif
using System;
using System.IO;
using System.Linq;

namespace Longbow.Logging
{
    public static class TestHelper
    {
#if NETCore
        /// <summary>
        /// 
        /// </summary>
        public static void TriggerAppSettingsFileChange(IConfiguration configuration, Action changeCallback)
        {
            ChangeToken.OnChange(() => configuration.GetReloadToken(), () => changeCallback.Invoke());
            var root = (ConfigurationRoot)configuration;
            root.Reload();
        }
#endif

        public static string RetrieveSolutionPath()
        {
            var dirSeparator = Path.DirectorySeparatorChar;
#if NETCore
            var paths = AppContext.BaseDirectory.SpanSplit($"{dirSeparator}.vs{dirSeparator}");
#else
            var paths = AppDomain.CurrentDomain.BaseDirectory.Split(new string[] { $"{dirSeparator}.vs{dirSeparator}" }, StringSplitOptions.RemoveEmptyEntries);
#endif
            return paths.Count() > 1 ? paths[0] : Path.Combine(AppContext.BaseDirectory, $"..{dirSeparator}..{dirSeparator}..{dirSeparator}..{dirSeparator}");
        }
    }
}
